package com.shiroha.user.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.shiroha.common.pojo.dto.UserDTO;
import com.shiroha.user.mapper.CachedUserMapper;
import com.shiroha.user.pojo.CachedUser;
import com.shiroha.user.service.ICachedUserService;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class CachedUserServiceImpl extends ServiceImpl<CachedUserMapper, CachedUser> implements ICachedUserService {

    /**
     * 保存用户
     *
     * @param userDTO DTO对象
     */
    @Override
    public Boolean saveCachedUser(UserDTO userDTO) {
        return this.save(toCachedUser(userDTO));
    }

    /**
     * 更新用户
     * @param userDTO DTO对象
     */
    @Override
    public Boolean updateCachedUser(UserDTO userDTO) {
        return this.updateById(toCachedUser(userDTO));
    }

    private CachedUser toCachedUser(UserDTO userDTO) {
        return CachedUser.builder()
                .id(UUID.fromString(userDTO.getId()))
                .username(userDTO.getUsername())
                .email(userDTO.getEmail())
                .avatar(userDTO.getAvatar())
                .gender(userDTO.getGender())
                .build();
    }
}
